import { getWeekDateNew } from '/js/util.js'
let list1 = [{
  name: '老师名字',
  value: 'Mia老师'
}, {
  name: '开始时间',
  value: '2021.09.06星期三12：00'
}, {
  name: '教程名',
  value: 'xxx'
}, {
  name: '第几节课',
  value: '第5课'
}, {
  name: '标题',
  value: '现在几点了'
}, {
  name: '页数',
  value: '12-13'
}, {
  name: '重点句型',
  value: 'Mia老师'
}, {
  name: '语法项目',
  value: 'Mia老师'
}, {
  name: '作业',
  value: '描述你的房间有什么'
}, {
  name: '下次何处开始',
  value: '第6课，p13页'
},{
  name: '老师感想',
  value: '第6课，p13页'
},]

let list2 = [{
  name: '老师名字',
  value: JSON.parse(localStorage.getItem('user')).user_name
}, {
  name: '开始时间',
  value: `${new Date().getFullYear()}.${new Date().getMonth() + 1}.${new Date().getDate()}${getWeekDateNew(new Date().getDay() === 0 ? 7 : new Date().getDay())}${new Date().getHours()}:00`
}, {
  name: '教程名',
  value: '',
  dataset : 'tutorialname',
}, {
  name: '第几节课',
  value: '',
  dataset : 'whatclass',
}, {
  name: '标题',
  value: '',
  dataset : 'title',
}, {
  name: '页数',
  value: '',
  dataset : 'pagequantity',
}, {
  name: '重点句型',
  value: '',
  dataset : 'keysentences',
}, {
  name: '语法项目',
  value: '',
  dataset : 'grammaritem',
}, {
  name: '作业',
  value: '',
  dataset : 'task',
}, {
  name: '下次何处开始',
  value: '',
  dataset : 'startnexttime',
},{
  name: '老师感想',
  value: '',
  dataset : 'teacherreflections'
},]


let params = {
  studentid:  window.location.href.split('/')[6].split('?')[1].split('&')[0].split('=')[1],
  teacherid: JSON.parse(localStorage.getItem('user')).user_id,
  teachername: JSON.parse(localStorage.getItem('user')).user_name,//老师名称
  createtime: `${new Date().getFullYear()}-${new Date().getMonth() + 1 < 10 ? '0' + new Date().getMonth() + 1 : new Date().getMonth() + 1}-${new Date().getDate() < 10 ? '0' + new Date().getDate() : new Date().getDate()} ${new Date().getHours()}:00:00`,//上课开始时间
  tutorialname: '',//教材名
  whatclass: '',//第几节课
  title: '',//标题
  pagequantity: '',//页数
  keysentences: '',//重点句型
  grammaritem: '',//语法项目
  task: '',//作业
  startnexttime: '',//下次何处开始
  teacherreflections: '',//老师感想
  state: '0',//状态
  timeslot: `${new Date().getHours() < 10 ? '0' + new Date().getHours() : new Date().getHours()}:00:00`,//上课时间准确点
  weekji: new Date().getDay() === 0 ? 7 : new Date().getDay(),//星期几
  curriculumid: window.location.href.split('/')[6].split('?')[1].split('&')[1].split('=')[1],
  cuyear: new Date().getFullYear(),
  cuday: new Date().getDate(),
  cumonth: new Date().getMonth() + 1,
}

let createHistoryWrap = async () => {
  await getCourseHistory()
  $('.detail-content')[0] && $('.detail-content')[0].remove()
  //老师课程记录 内容
  let section_content = document.createElement("section");
  //老师课程记录 内容 列表
  let section_footer = document.createElement("section");
  section_content.className = "detail-content";
  section_footer.className = "detail-content-list-wrap";
  section_content.innerHTML = `
        <ul class="detail-content-menu">
            <li class="detail-content-menu-active last-content">上次的课程记录</li>
            <li class="detail-content-menu-deactive current-content">这次的课程记录</li>
        </ul>
    `;
  for (let i = 0; i < list1.length; i++) {
    let s2 = document.createElement("section");
    s2.className = "detail-content-list";
    s2.innerHTML = `
            <div class="detail-content-list-title">${list1[i].name}</div>
            <div class="detail-content-list-subtitle">${list1[i].value}</div>
        `;
    section_footer.appendChild(s2);
  }
  $(".section-course_history-detail")[0].appendChild(section_content);
  section_content.appendChild(section_footer);
  getLastHistory()
  getCurrentHistory()

};

let getLastHistory = () => {
  $('.last-content')[0].addEventListener('click', () => {
    $('.last-content')[0].className = 'last-content detail-content-menu-active'
    $('.current-content')[0].className = 'current-content detail-content-menu-deactive'
    $(".detail-content-list-wrap")[0].innerHTML = ''
    for (let i = 0; i < list1.length; i++) {
      let s2 = document.createElement("section");
      s2.className = "detail-content-list";
      s2.innerHTML = `
          <div class="detail-content-list-title">${list1[i].name}</div>
          <div class="detail-content-list-subtitle">${list1[i].value}</div>
      `;
      $('.detail-content-list-wrap')[0].appendChild(s2);
    }
  })
}

let getCurrentHistory = () => {
  $('.current-content')[0].addEventListener('click', () => {

    $('.last-content')[0].className = 'last-content detail-content-menu-deactive'
    $('.current-content')[0].className = 'current-content detail-content-menu-active'
    $(".detail-content-list-wrap")[0].innerHTML = ''
    for (let i = 0; i < list2.length; i++) {
      let s2 = document.createElement("section");
      s2.className = "detail-content-list";
      s2.innerHTML = `
          <div class="detail-content-list-title">${list2[i].name}</div>
          ${i < 2 ? `<div class="detail-content-list-subtitle">${list2[i].value}</div>` : `<input data-name=${list2[i].dataset ? list2[i].dataset : ''} class="detail-content-list-subtitle" value='${list2[i].value}' style='width:90%'></input>`}  
      `;
      $('.detail-content-list-wrap')[0].appendChild(s2);
    }
    updateCurrentHistory()
  })
}


let getCourseHistory = async () => {
  let params = {}
  if (window.location.href.split('/')) {
    let location = window.location.href.split('/')[6].split('?')[1].split('&')
    location.forEach(item => {
      let key = item.split('=')[0]
      let value = item.split('=')[1]
      params[key] = value
    })
    let res = await ajax('student_query_curriculum_record', params, 'post')
    if (res.data.length) {
      res.data.forEach(item => {
        item.time = new Date(`${item.cuyear}-${item.cumonth}-${item.cuday} ${item.timeslot}`).getTime()
      })
      res.data.sort((a, b) => { return b.time - a.time })
      if (new Date(res.data[0].time).getMonth() === new Date().getMonth() && new Date(res.data[0].time).getDate() === new Date().getDate() && new Date(res.data[0].time).getHours() === new Date().getHours()) {
        list2[0].value = res.data[0].teachername + '老师'
        list2[1].value = res.data[0].createtime
        list2[2].value = res.data[0].tutorialname + '教程'
        list2[3].value = res.data[0].whatclass + '课'
        list2[4].value = res.data[0].title
        list2[5].value = res.data[0].pagequantity + '页'
        list2[6].value = res.data[0].keysentences
        list2[7].value = res.data[0].grammaritem
        list2[8].value = res.data[0].task
        list2[9].value = res.data[0].startnexttime
        list2[10].value = res.data[0].teacherreflections

        if(res.data[1]){
          list1[0].value = res.data[1].teachername + '老师'
          list1[1].value = res.data[1].createtime
          list1[2].value = res.data[1].tutorialname + '教程'
          list1[3].value = res.data[1].whatclass + '课'
          list1[4].value = res.data[1].title
          list1[5].value = res.data[1].pagequantity + '页'
          list1[6].value = res.data[1].keysentences
          list1[7].value = res.data[1].grammaritem
          list1[8].value = res.data[1].task
          list1[9].value = res.data[1].startnexttime
          list1[10].value = res.data[1].teacherreflections
        }
        
      } else {
        list1[0].value = res.data[0].teachername + '老师'
        list1[1].value = res.data[0].createtime
        list1[2].value = res.data[0].tutorialname + '教程'
        list1[3].value = res.data[0].whatclass + '课'
        list1[4].value = res.data[0].title
        list1[5].value = res.data[0].pagequantity + '页'
        list1[6].value = res.data[0].keysentences
        list1[7].value = res.data[0].grammaritem
        list1[8].value = res.data[0].task
        list1[9].value = res.data[0].startnexttime
        list1[10].value = res.data[0].teacherreflections
      }

    }
    console.log(res)
  }

}

let updateCurrentHistory = () => {
  $('.detail-content-list-subtitle').each(function () {
    $(this).on('keyup',async (e) => {
      if (e.keyCode == "13") {
        let obj = {
          [`${e.target.dataset.name}`] : `${e.target.value}`
        }
        Object.assign(params,obj)
        let res = await ajax('order_modification',params,'post')
        console.log(res,'ss')
      }
      
    })
  })
}

export { createHistoryWrap };
